package com.sxbbc.common.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sxbbc.common.core.entity.jz.WLogs;
import com.sxbbc.common.core.mapper.jz.WLogsMapper;
import com.sxbbc.common.core.service.IWLogsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sxbbc.common.core.utils.BasePage;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 仓库操作日志 服务实现类
 * </p>
 *
 * @author leehao
 * @since 2019-07-01
 */
@Service
public class WLogsServiceImpl extends ServiceImpl<WLogsMapper, WLogs> implements IWLogsService {

    @Override
    public Page<WLogs> queryLogList(BasePage<WLogs> page, WLogs log) {
        QueryWrapper<WLogs> wrapper = queryWrapper(log);
        if (page.isPaging()) {
            super.page(page, wrapper);
        } else {
            List<WLogs> list = super.list(wrapper);
            page.setRecords(list);
        }
        return page;
    }

    private QueryWrapper<WLogs> queryWrapper(WLogs log) {
        QueryWrapper<WLogs> wrapper = new QueryWrapper<>();
        wrapper.eq("wid",log.getWid());
        wrapper.orderByDesc("createtime");
        if (StringUtils.isNotBlank(log.getQueryTime())) {
            String[] times = log.getQueryTime().split("~");
            wrapper.apply("date_format(createtime,'%Y-%m-%d') >= {0}",times[0].trim());
            wrapper.apply("date_format(createtime,'%Y-%m-%d') <= {0}",times[1].trim());
        }
        return wrapper;
    }
}
